"""
功能函数
"""
import os, datetime
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.header import Header

from common.get_config import ReadConfig
from common.get_log import BotLogger

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
logger = BotLogger(os.path.basename(__file__), logname=1).getLogger()

now_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
env = ReadConfig().get_Url('base_url')

def send_email(project,testers,report):
    # 设置登录及服务器信息
    # mail_host = 'smtp.163.com'
    mail_host = 'smtp.workai.com.cn'
    mail_user = 'gaojp@workai.com.cn'
    mail_pass = '15611298166Gjp'
    sender = 'gaojp@workai.com.cn'
    receivers = ['gaojp@workai.com.cn', 'jack_roc@126.com']

    # 设置eamil信息
    # 添加一个MIMEmultipart类，处理正文及附件
    message = MIMEMultipart()
    message['From'] = sender
    message['To'] = receivers[0]
    # message['Subject'] = 'title'  # 主题
    # message['Subject'] = Header('自定义信件主题'.decode('utf-8')).encode()
    message['Subject'] = Header('This is a test', 'utf-8')
    # message['From'] = '%s <%s>' % (Header('自定义发信昵称'.decode('utf-8')).encode(), username)
    # message['Reply-to'] = replyto
    # message['Message-id'] = email.utils.make_msgid()
    # message['Date'] = email.utils.formatdate()
    # 推荐使用html格式的正文内容，这样比较灵活，可以附加图片地址，调整格式等
    # with open('abc.html','r') as f:
    #     content = f.read()
    # #设置html格式参数
    mail_content = " <p>项目名称 ： {}</p> <p>测试环境 ： {}</p> <p>测试时间 ： {}</p> <p>测试人员 ： {}</p> <p>测试报告 ： {}</p>".format(
        project, env, now_time, testers, report)

    part1 = MIMEText(mail_content, 'html', 'utf-8')
    # 添加一个txt文本附件
    # with open('abc.txt', 'r')as h:
    #     content2 = h.read()
    # 设置txt参数
    # part2 = MIMEText(content2, 'plain', 'utf-8')
    # 附件设置内容类型，方便起见，设置为二进制流
    # part2['Content-Type'] = 'application/octet-stream'
    # 设置附件头，添加文件名
    # part2['Content-Disposition'] = 'attachment;filename="abc.txt"'
    # 添加照片附件
    # with open('1.jpg', 'rb')as fp:
    #     picture = MIMEImage(fp.read())
    #     与txt文件设置相似
        # picture['Content-Type'] = 'application/octet-stream'
        # picture['Content-Disposition'] = 'attachment;filename="1.jpg"'
    # 将内容附加到邮件主体中
    message.attach(part1)
    # message.attach(part2)
    # message.attach(picture)

    # 登录并发送
    try:
        smtpObj = smtplib.SMTP()
        smtpObj.connect(mail_host, 25)
        smtpObj.login(mail_user, mail_pass)
        smtpObj.sendmail(
            sender, receivers, message.as_string())
        print('success')
        logger.info('发送邮件成功')
        smtpObj.quit()
    except smtplib.SMTPException as e:
        print('error', e)
        logger.info(str(e))
        logger.info('发送邮件失败')

if __name__ == '__main__':

    if not os.path.exists('F:/OFFICE/CODE/ui_automated/report/login_html/'):
        print('没有报告')

    else:
        print('报告存在')
        send_email('HRO添加客户','高杰鹏','http://192.168.137.1:56917/index.html')